import csv

prefixes = ['', 'pt_', 'exit_']
file_stem = '1616split'

for prefix in prefixes:
    data = {}
    with open('./Dropbox/Coalitions/' + prefix + 'cueing_data' + file_stem + '.csv', 'r') as f:
        response_reader = csv.reader(f, delimiter = ',', quotechar = '"')
        response_reader.next()
        
        for row in response_reader:
            legA = row[15]
            legB = row[16]
            congress = row[6]
            committee = row[14]
            
            if committee not in data:
                data[committee] = {}
            if congress not in data[committee]:
                data[committee][congress] = []
            data[committee][congress].append([legA, legB])
            
    already_in = {}
    with open('./Dropbox/Coalitions/' + prefix + 'cueing_data' + file_stem + '.csv', 'r') as f:
        response_reader = csv.reader(f, delimiter = ',', quotechar = '"')
        with open('./Dropbox/Coalitions/' + prefix + 'cueing_data' + file_stem + 'weighted.csv', 'w') as outf:
            response_writer = csv.writer(outf, delimiter = ',', quotechar = '"')
            firstrow = response_reader.next()
            firstrow.append('weights')
            response_writer.writerow(firstrow)
            for row in response_reader:
                legA = row[15]
                legB = row[16]
                congress = row[6]
                committee = row[14]
                
                if committee not in already_in:
                    already_in[committee] = {}
                if congress not in already_in[committee]:
                    already_in[committee][congress] = []
                if set([legA, legB, congress, committee]) not in already_in[committee][congress]:
                    weight = data[committee][congress].count([legA, legB]) + \
                    data[committee][congress].count([legB, legA])
                    row[15] = legA
                    row[16] = legB
                    row.append(weight)
                    response_writer.writerow(row)
                    already_in[committee][congress].append(set([legA, legB, congress, committee]))